#!/usr/bin/python
import unittest
import os
import sys

from uya.routerconf import validators
from uya.routerconf.configmod.configobj import validate

class TestValidators(unittest.TestCase):
    def test_validator_route(self):
        self.assertTrue(validators.route("10.0.0.1/8 10.0.1.2 5"))
        self.assertTrue(validators.route("10.0.0.1/24 10.255.1.2 1"))
        self.assertTrue(validators.route("10.0.0.1/24 10.255.1.2 20", maxcost=20))
        self.assertRaises(validate.VdtTypeError, validators.route, 
            "invalid value")
        self.assertRaises(validate.VdtTypeError, validators.route, 
            "a.0.0.1/24 10.255.1.2 2")
        self.assertRaises(validate.VdtTypeError, validators.route, 
            "300.0.0.1/24 10.255.1.2 2")
        self.assertRaises(validate.VdtTypeError, validators.route, 
            "192.0.0.1/100 10.255.1.2 2")
        self.assertRaises(validate.VdtTypeError, validators.route, 
            "10.0.0.1/24 10.255.1.2 20", maxcost=19)

    def test_validator_list_route(self):
        self.assertTrue(validators.route_list(
            ["10.0.0.1/8 10.0.1.2 5", "10.0.0.1/8 10.0.1.2 5"]))
        self.assertRaises(validate.VdtTypeError, validators.route_list,
            ["300.0.0.1/8 10.0.1.2 5", "10.0.0.1/8 10.0.1.2 5"])
        self.assertRaises(validate.VdtTypeError, validators.route_list,
            "10.0.0.1/8 10.0.1.2 5")

    def test_validator_ospf_network(self):
        self.assertTrue(validators.ospf_network("10.0.0.1/8 area 10.0.1.2"))
        self.assertTrue(validators.ospf_network("192.0.0.1/24 area 10.0.100.2"))
        self.assertRaises(validate.VdtTypeError, validators.ospf_network, 
            "invalid value")
        self.assertRaises(validate.VdtTypeError, validators.ospf_network, 
            "300.0.0.1/24 area 10.0.100.2")

    def test_validator_ospf_network_list(self):
        self.assertTrue(validators.ospf_network_list(
            ["10.0.0.1/8 area 10.0.1.2", "10.0.0.1/8 area 10.0.1.3"]))
        self.assertRaises(validate.VdtTypeError, validators.ospf_network_list,
            ["300.0.0.1/8 area 10.0.1.2", "10.0.0.1/8 area 10.0.1.3"])
        self.assertRaises(validate.VdtTypeError, validators.ospf_network_list,
            "100.0.0.1/8 area 10.0.1.2")

    def test_validator_ospf_area(self):
        self.assertTrue(validators.ospf_area("10.0.0.1 range 10.0.1.2/10"))
        self.assertTrue(validators.ospf_area("192.0.0.1 range 10.0.100.2/10"))
        self.assertRaises(validate.VdtTypeError, validators.ospf_area, 
            "invalid value")
        self.assertRaises(validate.VdtTypeError, validators.ospf_area, 
            "392.0.0.1 range 10.0.100.2/10")

    def test_validator_ospf_area_list(self):
        self.assertTrue(validators.ospf_area_list(
            ["192.0.0.1 range 10.0.100.2/10", "192.0.0.1 range 10.0.100.3/10"]))
        self.assertRaises(validate.VdtTypeError, validators.ospf_area_list,
            ["392.0.0.1 range 10.0.100.2/10", "192.0.0.1 range 10.0.100.3/10"])
        self.assertRaises(validate.VdtTypeError, validators.ospf_area_list,
            "392.0.0.1 range 10.0.100.2/10")

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestValidators)

if __name__ == '__main__':
    unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
